<?php
/**
 * Created by PhpStorm.
 * User: kxy
 * Date: 2018/8/20
 * Time: 下午9:07
 */

namespace App\Exports;

use App\Models\Order\Order;
use Illuminate\Database\Eloquent\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;

class OrderExport implements FromCollection
{

    private $orders;

    public function __construct(Collection $collection)
    {
        $this->orders = $collection;
    }

    public function collection()
    {
        $title = new Collection();
        $title->add([
            'number'     => '订单号',
            'title'      => '课程名称',
            'teacher'    => '教师',
            'total'      => '价格',
            'name'       => '昵称',
            'tel'        => '电话',
            'province'   => '省',
            'city'       => '市',
            'privilege'  => '区',
            'created_at' => '下单时间',
        ]);
        $this->orders->each(function (Order $order) use ($title) {
            $title->add([
                'number'     => $order->number,
                'title'      => $order->item->video->title,
                'teacher'    => $order->item->video->teacher->name,
                'total'      => $order->total,
                'name'       => $order->author->info->name,
                'tel'        => $order->author->tel,
                'province'   => $order->author->info->province,
                'city'       => $order->author->info->city,
                'privilege'  => $order->author->info->privilege,
                'created_at' => $order->created_at->toDateTimeString()
            ]);
        });
        return $title;
    }
}